import { createRouter, createWebHistory } from 'vue-router'

const pages = import.meta.glob('../views/**/*.vue')

const children = Object.entries(pages).map(([path, component]) => {
  path = path.replace('../views', '').replace('index.vue', '') || '/'
  const name = path.split('/').filter(Boolean).join('-').replace('.vue', '') || 'index'
  return {
    path,
    name,
    component
  }
})

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/Login/index.vue')
  },
  {
    name: 'menu',
    path: '/menu',
    component: () => import('@/layout/index.vue'),
    children
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

export default router
